//
//  ICMessageFrame.m
//  ICome
//
//  Created by ENN on 16/3/11.
//  Copyright © 2016年 iCom. All rights reserved.
//

#import "ICMessageFrame.h"
#import "ICMessageModel.h"
#import "NSString+Extension.h"
#import "ICMediaManager.h"
#import "UIImage+Extension.h"
#import "ICMessageHelper.h"
#import "ICVideoManager.h"
#import "ICMessageDatabase.h"
#import "ICNotifyMsgCell.h"

#define MESSAGEDEFAULTWIDTH 253

@implementation ICMessageFrame

- (void)setModel:(ICMessageModel *)model
{
    _model = model;
    CGFloat headToView    = 10;     // 头像右边距
    CGFloat headToBubble  = 3;      // 头像与气泡的间距
    CGFloat headWidth     = 45;     // 头像宽度
    CGFloat cellMargin    = 10;     // 头像上边距
    CGFloat bubblePadding = 10;     // 气泡与文本内容间距
    CGFloat chatLabelMax  = App_Frame_Width - headWidth - 100; // 文字的最大宽度
    CGFloat arrowWidth    = 6;      // 气泡箭头
    CGFloat topViewH      = 18;     // 顶部view高度
    CGFloat cellMinW      = 60;     // cell的最小宽度值,针对文本
    
    CGSize timeSize  = [@"22:44  " sizeWithMaxWidth:chatLabelMax andFont:[UIFont systemFontOfSize:14]];
    headWidth = headWidth*Level;
    if (model.isSender) {
        cellMinW = timeSize.width + arrowWidth + bubblePadding*2;
        CGFloat headX = App_Frame_Width - headToView - headWidth;
        _headImageViewF = CGRectMake(headX, cellMargin, headWidth, headWidth);
        if ([model.message.type isEqualToString:TypeText] || [model.message.type isEqualToString:TypeAtText]) { // 文字
            CGSize chateLabelSize = [model.message.content sizeWithMaxWidth:chatLabelMax andFont:MessageFont];
            CGSize bubbleSize     = CGSizeMake(chateLabelSize.width + bubblePadding * 2 + arrowWidth, chateLabelSize.height + bubblePadding * 2);
            CGSize topViewSize    = CGSizeMake(cellMinW+bubblePadding*2, topViewH);
            _bubbleViewF          = CGRectMake(CGRectGetMinX(_headImageViewF) - headToBubble - bubbleSize.width, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
            CGFloat x             = CGRectGetMinX(_bubbleViewF)+bubblePadding;
            _topViewF             = CGRectMake(CGRectGetMinX(_headImageViewF) - headToBubble - topViewSize.width-headToBubble-5, cellMargin,topViewSize.width,topViewSize.height);
            _chatLabelF           = CGRectMake(x, topViewH + cellMargin + bubblePadding, chateLabelSize.width, chateLabelSize.height);
        } else if ([model.message.type isEqualToString:TypePic]) { // 图片
            CGSize imageSize = CGSizeZero;
            
            NSDictionary *lnk = [NSDictionary initWithJsonString:model.message.lnk];
            CGSize imgSize = CGSizeMake([lnk[@"w"] floatValue], [lnk[@"h"] floatValue]);
            imageSize          = [self handleImage:imgSize];
            imageSize.width        = imageSize.width > timeSize.width ? imageSize.width : timeSize.width;
            CGSize topViewSize     = CGSizeMake(imageSize.width-arrowWidth, topViewH);
            CGSize bubbleSize      = CGSizeMake(imageSize.width, imageSize.height);
            CGFloat bubbleX        = CGRectGetMinX(_headImageViewF)-headToBubble-bubbleSize.width;
            _bubbleViewF           = CGRectMake(bubbleX, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
            CGFloat x              = CGRectGetMinX(_bubbleViewF);
            _topViewF             = CGRectMake(x, cellMargin,topViewSize.width,topViewSize.height);
            _picViewF              = CGRectMake(x, cellMargin+topViewH, imageSize.width, imageSize.height);
        } else if ([model.message.type isEqualToString:TypeVoice]) { // 语音消息
            
            CGFloat bubbleViewW = [self getVoiceWidth:model.message.lnk];
            _bubbleViewF = CGRectMake(CGRectGetMinX(_headImageViewF) - headToBubble - bubbleViewW, cellMargin+topViewH, bubbleViewW, 40);
            _topViewF               = CGRectMake(CGRectGetMinX(_bubbleViewF), cellMargin, bubbleViewW - arrowWidth, topViewH);
            _durationLabelF         = CGRectMake(CGRectGetMinX(_bubbleViewF)+ bubblePadding , cellMargin + 10+topViewH, 50, 20);
            _voiceIconF = CGRectMake(CGRectGetMaxX(_bubbleViewF) - 26, cellMargin + 11.75 + topViewH, 11, 16.5);// - 20 22
        }  else if ([model.message.type isEqualToString:TypeVideo]) { // 视频信息
            CGSize imageSize       = CGSizeZero;
            
            NSDictionary *lnk = [NSDictionary initWithJsonString:model.message.lnk];
            CGSize imgSize = CGSizeMake([lnk[@"w"] floatValue], [lnk[@"h"] floatValue]);
            
            float scale = 1.5;
            if (imgSize.width != 0 && imgSize.height != 0) {
                scale = imgSize.height/imgSize.width;
            }
            imageSize = CGSizeMake(150, 140*scale);
            
            CGSize bubbleSize = CGSizeMake(imageSize.width, imageSize.height);
            _bubbleViewF = CGRectMake(CGRectGetMinX(_headImageViewF)-headToBubble-bubbleSize.width, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
            CGSize topViewSize     = CGSizeMake(imageSize.width-arrowWidth, topViewH);
            CGFloat x              = CGRectGetMinX(_bubbleViewF);
            _topViewF              = CGRectMake(x, cellMargin, topViewSize.width, topViewSize.height);
            _picViewF              = CGRectMake(x, cellMargin+topViewH, imageSize.width, imageSize.height);
        } else if ([model.message.type isEqualToString:TypeFile]) {
            CGSize bubbleSize = CGSizeMake(MESSAGEDEFAULTWIDTH, 95.0);
            _bubbleViewF = CGRectMake(CGRectGetMinX(_headImageViewF)-headToBubble-bubbleSize.width, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
            CGSize topViewSize     = CGSizeMake(bubbleSize.width-arrowWidth, topViewH);
            CGFloat x              = CGRectGetMinX(_bubbleViewF);
            _topViewF              = CGRectMake(x, cellMargin, topViewSize.width, topViewSize.height);
            _picViewF              = CGRectMake(x, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
        } else if ([model.message.type isEqualToString:TypePicText]) {
            CGSize bubbleSize = CGSizeMake(MESSAGEDEFAULTWIDTH, 120.0);
            _bubbleViewF = CGRectMake(CGRectGetMinX(_headImageViewF)-headToBubble-bubbleSize.width, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
            CGSize topViewSize     = CGSizeMake(bubbleSize.width-arrowWidth, topViewH);
            CGFloat x              = CGRectGetMinX(_bubbleViewF);
            _topViewF              = CGRectMake(x, cellMargin, topViewSize.width, topViewSize.height);
            _picViewF              = CGRectMake(x, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
        } else if ([model.message.type isEqualToString:TypeRedPacket]) {
            CGSize bubbleSize = CGSizeMake(214, 61.0);
            _bubbleViewF = CGRectMake(CGRectGetMinX(_headImageViewF)-headToBubble-bubbleSize.width, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
            CGSize topViewSize     = CGSizeMake(bubbleSize.width-arrowWidth, topViewH);
            CGFloat x              = CGRectGetMinX(_bubbleViewF);
            _topViewF              = CGRectMake(x, cellMargin, topViewSize.width, topViewSize.height);
            _picViewF              = CGRectMake(x, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
        } else if ([model.message.type isEqualToString:TypeVideoConference]) {
            CGSize bubbleSize = CGSizeMake(MESSAGEDEFAULTWIDTH, 80.0);
            _bubbleViewF = CGRectMake(CGRectGetMinX(_headImageViewF)-headToBubble-bubbleSize.width, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
            CGSize topViewSize     = CGSizeMake(bubbleSize.width-arrowWidth, topViewH);
            CGFloat x              = CGRectGetMinX(_bubbleViewF);
            _topViewF              = CGRectMake(x, cellMargin, topViewSize.width, topViewSize.height);
            _picViewF              = CGRectMake(x, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
        } else if ([model.message.type isEqualToString:TypeDrtNews]) {
            CGSize bubbleSize = CGSizeMake(MESSAGEDEFAULTWIDTH, 110.0);
            _bubbleViewF = CGRectMake(CGRectGetMinX(_headImageViewF)-headToBubble-bubbleSize.width, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
            CGSize topViewSize     = CGSizeMake(bubbleSize.width-arrowWidth, topViewH);
            CGFloat x              = CGRectGetMinX(_bubbleViewF);
            _topViewF              = CGRectMake(x, cellMargin, topViewSize.width, topViewSize.height);
            _picViewF              = CGRectMake(x, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
        } else if ([model.message.type isEqualToString:TypeTextURL]) {
            CGSize chateLabelSize = [model.message.content sizeWithMaxWidth:chatLabelMax andFont:MessageFont];
            CGSize bubbleSize     = CGSizeMake(chateLabelSize.width + bubblePadding * 2 + arrowWidth, chateLabelSize.height + bubblePadding * 2);
            CGSize topViewSize    = CGSizeMake(cellMinW+bubblePadding*2, topViewH);
            _bubbleViewF          = CGRectMake(CGRectGetMinX(_headImageViewF) - headToBubble - bubbleSize.width, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
            CGFloat x             = CGRectGetMinX(_bubbleViewF)+bubblePadding;
            _topViewF             = CGRectMake(CGRectGetMinX(_headImageViewF) - headToBubble - topViewSize.width-headToBubble-5, cellMargin,topViewSize.width,topViewSize.height);
            _chatLabelF           = CGRectMake(x, topViewH + cellMargin + bubblePadding, chateLabelSize.width, chateLabelSize.height);
        } else if ([model.message.type isEqualToString:TypeSysTop]) {
            CGSize bubbleSize = CGSizeMake(MESSAGEDEFAULTWIDTH, 76.0);
            _bubbleViewF = CGRectMake(CGRectGetMinX(_headImageViewF)-headToBubble-bubbleSize.width, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
            CGSize topViewSize     = CGSizeMake(bubbleSize.width-arrowWidth, topViewH);
            CGFloat x              = CGRectGetMinX(_bubbleViewF);
            _topViewF              = CGRectMake(x, cellMargin, topViewSize.width, topViewSize.height);
            _picViewF              = CGRectMake(x, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
        }
        CGFloat activityX = _bubbleViewF.origin.x-40;
        CGFloat activityY = (_bubbleViewF.origin.y + _bubbleViewF.size.height)/2 - 5;
        CGFloat activityW = 40;
        CGFloat activityH = 40;
        _activityF        = CGRectMake(activityX, activityY, activityW, activityH);
        _retryButtonF     = _activityF;
    } else {    // 接收者
        _headImageViewF   = CGRectMake(headToView, cellMargin, headWidth, headWidth);
        NSString *senderName = @"";
        CGSize nameSize       = [senderName sizeWithMaxWidth:chatLabelMax andFont:[UIFont systemFontOfSize:14]];
        cellMinW = nameSize.width + 6 + timeSize.width; // 最小宽度
        
        if ([model.message.type isEqualToString:TypeText] || [model.message.type isEqualToString:TypeAtText]) {
            CGSize chateLabelSize = [model.message.content sizeWithMaxWidth:chatLabelMax andFont:MessageFont];
            CGSize topViewSize    = CGSizeMake(cellMinW+bubblePadding*2, topViewH);
            CGSize bubbleSize = CGSizeMake(chateLabelSize.width + bubblePadding * 2 + arrowWidth, chateLabelSize.height + bubblePadding * 2);
            
            _bubbleViewF  = CGRectMake(CGRectGetMaxX(_headImageViewF) + headToBubble, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
            CGFloat x     = CGRectGetMinX(_bubbleViewF) + bubblePadding + arrowWidth;
            _topViewF     = CGRectMake(CGRectGetMinX(_bubbleViewF)+arrowWidth, cellMargin, topViewSize.width, topViewSize.height);
            _chatLabelF   = CGRectMake(x, cellMargin + bubblePadding + topViewH, chateLabelSize.width, chateLabelSize.height);
        } else if ([model.message.type isEqualToString:TypePic]) {
            CGSize imageSize = CGSizeZero;
            
            NSDictionary *lnk = [NSDictionary initWithJsonString:model.message.lnk];
            CGSize imgSize = CGSizeMake([lnk[@"w"] floatValue], [lnk[@"h"] floatValue]);
            imageSize = [self handleImage:imgSize];
            imageSize.width        = imageSize.width > cellMinW ? imageSize.width : cellMinW;
            CGSize topViewSize     = CGSizeMake(imageSize.width-arrowWidth, topViewH);
            CGSize bubbleSize      = CGSizeMake(imageSize.width, imageSize.height);
            CGFloat bubbleX        = CGRectGetMaxX(_headImageViewF)+headToBubble;
            _bubbleViewF           = CGRectMake(bubbleX, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
            CGFloat x              = CGRectGetMinX(_bubbleViewF);
            _topViewF              = CGRectMake(x+arrowWidth, cellMargin, topViewSize.width, topViewSize.height);
            _picViewF              = CGRectMake(x, cellMargin+topViewH, imageSize.width, imageSize.height);
            
        } else if ([model.message.type isEqualToString:TypeVoice]) {   // 语音
            //            CGFloat bubbleViewW = cellMinW + 20; // 加上一个红点的宽度
            CGFloat bubbleViewW = [self getVoiceWidth:model.message.lnk];
            _bubbleViewF = CGRectMake(CGRectGetMaxX(_headImageViewF) + headToBubble, cellMargin+topViewH, bubbleViewW, 40);
            _topViewF    = CGRectMake(CGRectGetMinX(_bubbleViewF)+arrowWidth, cellMargin, bubbleViewW-arrowWidth, topViewH);
            _voiceIconF = CGRectMake(CGRectGetMinX(_bubbleViewF)+arrowWidth+bubblePadding, cellMargin + 11.75 + topViewH, 11, 16.5);
            _durationLabelF = CGRectMake(CGRectGetMaxX(_bubbleViewF) - bubblePadding - 50, cellMargin + 10 + topViewH, 50, 20);
            _redViewF = CGRectMake(CGRectGetMaxX(_bubbleViewF) + 6, CGRectGetMinY(_bubbleViewF) + _bubbleViewF.size.height*0.5-4, 8, 8);
        } else if ([model.message.type isEqualToString:TypeVideo]) {   // 视频
            CGSize imageSize       = CGSizeZero;
            NSDictionary *lnk = [NSDictionary initWithJsonString:model.message.lnk];
            CGSize imgSize = CGSizeMake([lnk[@"w"] floatValue], [lnk[@"h"] floatValue]);
            //            float scale = imgSize.height/imgSize.width;
            float scale = 1.5;
            if (imgSize.width != 0 && imgSize.height != 0) {
                scale = imgSize.height/imgSize.width;
            }
            imageSize = CGSizeMake(150, 140*scale);
            
            CGSize bubbleSize = CGSizeMake(imageSize.width, imageSize.height+topViewH);
            _bubbleViewF = CGRectMake(CGRectGetMaxX(_headImageViewF)+headToBubble, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
            CGSize topViewSize     = CGSizeMake(imageSize.width-arrowWidth, topViewH);
            CGFloat x              = CGRectGetMinX(_bubbleViewF);
            _topViewF              = CGRectMake(x+arrowWidth, cellMargin, topViewSize.width, topViewSize.height);
            _picViewF              = CGRectMake(x, cellMargin+topViewH, imageSize.width, imageSize.height);
        } else if ([model.message.type isEqualToString:TypeFile]) {
            CGSize bubbleSize = CGSizeMake(MESSAGEDEFAULTWIDTH, 95.0);
            _bubbleViewF = CGRectMake(CGRectGetMaxX(_headImageViewF)+headToBubble, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
            CGSize topViewSize     = CGSizeMake(bubbleSize.width-arrowWidth, topViewH);
            CGFloat x              = CGRectGetMinX(_bubbleViewF);
            _topViewF              = CGRectMake(x+arrowWidth, cellMargin, topViewSize.width, topViewSize.height);
            _picViewF              = CGRectMake(x, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
        } else if ([model.message.type isEqualToString:TypePicText]) {
            CGSize bubbleSize = CGSizeMake(MESSAGEDEFAULTWIDTH, 120.0);
            _bubbleViewF = CGRectMake(CGRectGetMaxX(_headImageViewF)+headToBubble, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
            CGSize topViewSize     = CGSizeMake(bubbleSize.width-arrowWidth, topViewH);
            CGFloat x              = CGRectGetMinX(_bubbleViewF);
            _topViewF              = CGRectMake(x+arrowWidth, cellMargin, topViewSize.width, topViewSize.height);
            _picViewF              = CGRectMake(x, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
        } else if ([model.message.type isEqualToString:TypeRedPacket]) {
            CGSize bubbleSize = CGSizeMake(214, 61.0);
            _bubbleViewF = CGRectMake(CGRectGetMaxX(_headImageViewF)+headToBubble, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
            CGSize topViewSize     = CGSizeMake(bubbleSize.width-arrowWidth, topViewH);
            CGFloat x              = CGRectGetMinX(_bubbleViewF);
            _topViewF              = CGRectMake(x+arrowWidth, cellMargin, topViewSize.width, topViewSize.height);
            _picViewF              = CGRectMake(x+arrowWidth, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
        } else if ([model.message.type isEqualToString:TypeVideoConference]) {
            CGSize bubbleSize = CGSizeMake(MESSAGEDEFAULTWIDTH, 80.0);
            _bubbleViewF = CGRectMake(CGRectGetMaxX(_headImageViewF)+headToBubble, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
            CGSize topViewSize     = CGSizeMake(bubbleSize.width-arrowWidth, topViewH);
            CGFloat x              = CGRectGetMinX(_bubbleViewF);
            _topViewF              = CGRectMake(x+arrowWidth, cellMargin, topViewSize.width, topViewSize.height);
            _picViewF              = CGRectMake(x, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
        } else if ([model.message.type isEqualToString:TypeDrtNews]) {
            CGSize bubbleSize = CGSizeMake(MESSAGEDEFAULTWIDTH, 110.0);
            _bubbleViewF = CGRectMake(CGRectGetMaxX(_headImageViewF)+headToBubble, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
            CGSize topViewSize     = CGSizeMake(bubbleSize.width-arrowWidth, topViewH);
            CGFloat x              = CGRectGetMinX(_bubbleViewF);
            _topViewF              = CGRectMake(x+arrowWidth, cellMargin, topViewSize.width, topViewSize.height);
            _picViewF              = CGRectMake(x, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
        } else if ([model.message.type isEqualToString:TypeTextURL]) {// 文字链接
            CGSize chateLabelSize = [model.message.content sizeWithMaxWidth:chatLabelMax andFont:MessageFont];
            CGSize topViewSize    = CGSizeMake(cellMinW+bubblePadding*2, topViewH);
            CGSize bubbleSize = CGSizeMake(chateLabelSize.width + bubblePadding * 2 + arrowWidth, chateLabelSize.height + bubblePadding * 2);
            
            _bubbleViewF  = CGRectMake(CGRectGetMaxX(_headImageViewF) + headToBubble, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
            CGFloat x     = CGRectGetMinX(_bubbleViewF) + bubblePadding + arrowWidth;
            _topViewF     = CGRectMake(CGRectGetMinX(_bubbleViewF)+arrowWidth, cellMargin, topViewSize.width, topViewSize.height);
            _chatLabelF   = CGRectMake(x, cellMargin + bubblePadding + topViewH, chateLabelSize.width, chateLabelSize.height);
        } else if ([model.message.type isEqualToString:TypeQuestion]) {// 机器人提醒
            CGSize bubbleSize = CGSizeMake(MESSAGEDEFAULTWIDTH, 110.0);
            
            NSDictionary *dict = [NSDictionary dictionaryWithJsonString:model.message.fileKey];
            CGSize titleSize = [dict[@"label"] sizeWithMaxWidth:MESSAGEDEFAULTWIDTH-50 andFont:[UIFont systemFontOfSize:15]];
            
            NSArray *contentList = dict[@"questions"];
            CGFloat contentHeight = 0;
            if (contentList.count > 0) {
                for(NSInteger i = 0;i < contentList.count; i++){
                    contentHeight += (24 + 15);
                }
            }
            
            bubbleSize.height = titleSize.height + contentHeight + 20;
            
            _bubbleViewF = CGRectMake(CGRectGetMaxX(_headImageViewF)+headToBubble, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
            CGSize topViewSize     = CGSizeMake(bubbleSize.width-arrowWidth, topViewH);
            CGFloat x              = CGRectGetMinX(_bubbleViewF);
            _topViewF              = CGRectMake(x+arrowWidth, cellMargin, topViewSize.width, topViewSize.height);
            _chatLabelF   = CGRectMake(x, cellMargin + bubblePadding + topViewH, bubbleSize.width, bubbleSize.height);
        } else if ([model.message.type isEqualToString:TypeSysTop]) {
            CGSize bubbleSize = CGSizeMake(MESSAGEDEFAULTWIDTH, 76.0);
            _bubbleViewF = CGRectMake(CGRectGetMaxX(_headImageViewF)+headToBubble, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
            CGSize topViewSize     = CGSizeMake(bubbleSize.width-arrowWidth, topViewH);
            CGFloat x              = CGRectGetMinX(_bubbleViewF);
            _topViewF              = CGRectMake(x+arrowWidth, cellMargin, topViewSize.width, topViewSize.height);
            _picViewF              = CGRectMake(x, cellMargin+topViewH, bubbleSize.width, bubbleSize.height);
        }
    }
    _cellHight = MAX(CGRectGetMaxY(_bubbleViewF), CGRectGetMaxY(_headImageViewF)) + cellMargin;
    if ([model.message.type isEqualToString:TypeSystem]) {
        CGSize size = [model.message.content sizeWithMaxWidth:[UIScreen mainScreen].bounds.size.width-40 andFont:[UIFont systemFontOfSize:14]];
        _cellHight = size.height+25;
    } else if ([model.message.type isEqualToString:TypeChance]) {
        
        NSDictionary *dict = [NSDictionary dictionaryWithJsonString:model.message.fileKey];
        
        CGSize titleSize = [dict[@"title"] sizeWithMaxWidth:[UIScreen mainScreen].bounds.size.width-40 andFont:[UIFont systemFontOfSize:17]];
        CGSize subjectSize = [dict[@"subject"] sizeWithMaxWidth:[UIScreen mainScreen].bounds.size.width-40 andFont:[UIFont systemFontOfSize:14]];
        NSArray *contentList = dict[@"content"];
        CGFloat contentHeight = contentList.count * 22;
        if (contentList.count > 0) {
            _cellHight = titleSize.height + subjectSize.height + contentHeight + 80;
        } else {
            _cellHight = titleSize.height + subjectSize.height + contentHeight + 70;
        }
    } else if ([model.message.type isEqualToString:TypeNotif]) {
        
        //dispatch_async(dispatch_get_main_queue(), ^{
        
        NSDictionary *dict = [NSDictionary dictionaryWithJsonString:model.message.fileKey];
        
        CGSize titleSize = [@"应用站位字符串" sizeWithMaxWidth:[UIScreen mainScreen].bounds.size.width-40 andFont:[UIFont systemFontOfSize:17]];
        if ([dict[@"showIcon"] intValue] == 1) {
            titleSize = CGSizeMake(0, 0);
        }
        NSString *summary = @"";
        if ([dict[@"summary"] isKindOfClass:[NSString class]]) summary = dict[@"summary"];
        CGFloat subjectHeight = [ICNotifyMsgCell contentLabelheight:summary];
        NSArray *contentList = dict[@"content"];
        CGFloat contentHeight = 0;
        if (contentList.count > 0) {
            for(NSInteger i = 0;i < contentList.count; i++){
                NSDictionary *dict = contentList[i];
                CGFloat Hight = [ICNotifyMsgCell calculateheight:dict[@"value"]];
                if(Hight > 22){
                    contentHeight += (Hight+7);
                }else{
                    contentHeight += 22;
                }
            }
        }
        if (contentList.count > 0) {
            NSInteger defHeight = 72;
            if ([dict[@"showIcon"] intValue] == 1) {
                defHeight = 45;
            }
            _cellHight = titleSize.height + subjectHeight + contentHeight + defHeight;
        } else {
            _cellHight = titleSize.height + subjectHeight + contentHeight + 63;
        }
        //   });
    } else if ([model.message.type isEqualToString:TypePending]) {
        _cellHight = 220;
    } else if ([model.message.type isEqualToString:TypeDrtNews] && [model.message.to isEqualToString:[NSString stringWithFormat:@"ntf_%@", [ICUser currentUser].eId]]) { // 通知中的资讯消息
        
        NSDictionary *dict = [NSDictionary dictionaryWithJsonString:model.message.fileKey];
        
        CGSize titleSize = [@"应用站位字符串" sizeWithMaxWidth:[UIScreen mainScreen].bounds.size.width-60 andFont:[UIFont systemFontOfSize:17]];
        
        NSString *content = @"";
        if (![ICTools stringEmpty:model.message.content]) content = model.message.content;
        CGSize subjectSize = [content sizeWithMaxWidth:[UIScreen mainScreen].bounds.size.width-60 andFont:[UIFont systemFontOfSize:16]];
        
        NSString *summary = @"";
        if ([dict[@"summary"] isKindOfClass:[NSString class]]) summary = dict[@"summary"];
        if (![ICTools stringEmpty:dict[@"filekey"]]) { // 资讯带图
            _cellHight = titleSize.height + subjectSize.height + 60 +  72;
        } else {
            CGSize summarySize = [summary sizeWithMaxWidth:[UIScreen mainScreen].bounds.size.width - 130 andFont:[UIFont systemFontOfSize:16]];
            _cellHight = titleSize.height + subjectSize.height + summarySize.height + 82;
        }
    } else if ([model.message.type isEqualToString:TypeActivity]) {
        _cellHight = 150;
    }
}

// 缩放，临时的方法
- (CGSize)handleImage:(CGSize)retSize
{
    if (retSize.width == 0 || retSize.height == 0) {
        return CGSizeMake(80, 80);
    }
    CGFloat scaleH = 0.22;
    CGFloat scaleW = 0.38;
    CGFloat height = 0;
    CGFloat width = 0;
    if (retSize.height / APP_Frame_Height + 0.16 > retSize.width / App_Frame_Width) {
        height = APP_Frame_Height * scaleH;
        width = retSize.width / retSize.height * height;
    } else {
        width = App_Frame_Width * scaleW;
        height = retSize.height / retSize.width * width;
    }
    return CGSizeMake(width, height);
}

/*
 *  规则：
 *  1s      70
 *  2-10s   递增10/s
 *  10-60s  递增0.5/s
 *  50s     开始倒计时
 */
- (CGFloat)getVoiceWidth:(NSString *)content {
    int duration = [content intValue];
    int s1 = 10; float s2 = 0.5;
    CGFloat width = 70;
    if (duration > 60) duration = 60;
    if (duration > 1 && duration <= 10) {
        duration -= 1;
        width += duration * s1;
    }else if (duration > 10 && duration <= 60) {
        width += (s1 - 1) * s1 + (duration - 10) * s2;
    }
    return width;
}

@end
